package com.kaiyidiannao.activity.user_detail;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;
import com.kaiyidiannao.activity.auction.AuctionDetailActivity;
import com.kaiyidiannao.adapter.MyBiddingAdapter;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.common.pulltorefresh_view.DragListView;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.Auction;
import com.kaiyidiannao.model.bean.UserBean;
import com.kaiyidiannao.model.req.model.GetMyBidding;
import com.kaiyidiannao.view.ActionBarView;
import com.taplinker.core.util.LogUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by mark on 2014/4/17.
 */
public class MyBidding extends BaseActionBarActivity implements Observer {

    private MyBiddingAdapter myBiddingAdapter = null;
    private List<Auction> biddingLists = null;
    private DragListView baseList;
    private ArrayList<UserBean> userBeans = DataCenter.userLogin;
    private View no_content_view;//没有数据的布局对象
    private ArrayList<Auction> tourBeans = DataCenter.auctions;
    private String userid;
    private int page = DataCenter.LISTVIEW_DEFAULT_PAGE; //当前页码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.base_info);
        // 添加观察者
        DataCenter.getInstance().addObserver(this);
        findActionBar();
        Log.d("tourBeans", tourBeans.size() + "");
        new GetMyBidding(userBeans.get(0).getUserid(), page).execute();
        showProgressDialog("正在加载");

        userid = DataCenter.userLogin.get(0).getUserid();

        baseList.setOnRefreshListener(new DragListView.OnRefreshLoadingMoreListener() {
            @Override
            public void onRefresh() {
                page = 1;
                if (DataCenter.userLogin.size() != 0) {
                    new GetMyBidding(userBeans.get(0).getUserid(), page).execute();
                } else {
                    showToast(getText(R.string.no_network).toString());
                }
            }

            @Override
            public void onLoadMore() {
                if (DataCenter.userLogin.size() != 0) {
                    new GetMyBidding(userBeans.get(0).getUserid(), ++page).execute();
                } else {
                    showToast(getText(R.string.no_network).toString());
                }
            }
        });

        baseList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Auction auction = tourBeans.get(position - 1);
                Intent intent = new Intent(MyBidding.this, AuctionDetailActivity.class);
//                Bundle bundle = new Bundle();
//                bundle.putSerializable("mybidding", auction);
//                intent.putExtras(bundle);
                intent.putExtra("itemid", auction.getItemid());
                intent.putExtra("catid", auction.getCatid());
                intent.putExtra("flag", "flag");
                startActivity(intent);
                DataCenter.mybidd_position = position - 1;
                DataCenter.PAGE = (position - 1) / 10 + 1;
                page = (position - 1) / 10 + 1;
            }
        });

        myBiddingAdapter = new MyBiddingAdapter(MyBidding.this, tourBeans);
        baseList.setAdapter(myBiddingAdapter);
        myBiddingAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onRestart() {
        new GetMyBidding(userBeans.get(0).getUserid(), page).execute();
        super.onRestart();
    }

    private void findActionBar() {
        actionBarView = (ActionBarView) findViewById(R.id.actionbar);
        actionBarView.setTitle("我的竞价");

        baseList = (DragListView) findViewById(R.id.baseList);
        no_content_view = findViewById(R.id.no_content_view);

    }

    @Override
    public void update(Observable observable, Object data) {
        DataCenter.flag flag = (DataCenter.flag) data;
        if (!HttpUtils.isConnected()) {
            baseList.onRefreshComplete();
            baseList.onLoadMoreComplete(false);
        }
        baseList.onLoadMoreComplete(false);
        baseList.onRefreshComplete();
        closeProgressDialog();
        if (flag == DataCenter.flag.GET_MY_BIDDING_SUCCESS
                || flag == DataCenter.flag.GET_MY_BIDDING_FAILED) { //类表数据

            String code = DataCenter.code;
            if (flag == DataCenter.flag.GET_MY_BIDDING_SUCCESS) {
                if (code.equals(DataCenter.successFlag)) {
                    if (tourBeans.size() == 0) {
                        no_content_view.setVisibility(View.VISIBLE);
                        baseList.setVisibility(View.INVISIBLE);
                        ((TextView) no_content_view.findViewById(R.id.notice_msg_tv)).setText("暂时没有数据");
                    } else {
                        no_content_view.setVisibility(View.GONE);
                        baseList.setVisibility(View.VISIBLE);
                    }

                } else {
                    Toast.makeText(MyBidding.this, DataCenter.msg, Toast.LENGTH_SHORT).show();
                    return;
                }
            }

            if (flag == DataCenter.flag.GET_MY_BIDDING_FAILED) {
                closeProgressDialog();
                //showToast(getString(R.string.no_network));
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.d(DataCenter.LOG_INFO, EditPassword.class.getSimpleName() + ".onDestroy");
        //删除观察者
        DataCenter.getInstance().deleteObserver(this);
    }

}



